<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%;">
    <el-col :span="20">
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex">
        <!-- 两个内容 -->
        <el-col :span="12">{{ treeNode.manager }}</el-col>
        <el-col :span="12">
          <!-- 下拉菜单 element -->
          <el-dropdown @command="TheMenuFeatures">
            <span>
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
// 导入删除模块接口
import { delDepartments } from '@/api/departments'
export default {
  props: {
    // 1. 接收外部传递的数据对象
    treeNode: {
      type: Object,
      required: true
    },
    // 2.用来做二级菜单的显示隐藏
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    TheMenuFeatures(command) {
      if (command === 'add') {
        //  1
        console.log('这是add，添加操作')
        this.$emit('addbm', this.treeNode) // 触发自定义事件
      } else if (command === 'edit') {
        console.log('edit，这是编辑操作')
        // 子组件自定义事件
        this.$emit('edit', this.treeNode)
        // 调用获取部门详情的接口，将需要编辑的部门显示已有的信息
      } else {
        // 1
        console.log('这是del，删除操作')
        console.log(this.treeNode)
        this.$confirm(`确定要删除 ${this.treeNode.introduce} 部门吗？`).then(() => {
          // 返回一个promise对象
          return delDepartments(this.treeNode.id)
        }).then(() => {
          // 删除成功之后进入这里
          console.log('删除成功')
          this.$emit('delDepts') // 触发自定义事件
          this.$message.success('删除部门成功')
        })
      }
    }
  }
}
</script>

<style>

</style>
